<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="包号" prop="pid">
      <el-input v-model="dataForm.pid" placeholder="包号"></el-input>
    </el-form-item>
    <el-form-item label="机构名称" prop="jname">
      <el-input v-model="dataForm.jname" placeholder="机构名称"></el-input>
    </el-form-item>
    <el-form-item label="省份城市" prop="city">
      <el-input v-model="dataForm.city" placeholder="省份城市"></el-input>
    </el-form-item>
    <el-form-item label="机构类型" prop="jtype">
      <el-input v-model="dataForm.jtype" placeholder="机构类型"></el-input>
    </el-form-item>
    <el-form-item label="机构级别" prop="jlevel">
      <el-input v-model="dataForm.jlevel" placeholder="机构级别"></el-input>
    </el-form-item>
    <el-form-item label="分包状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="分包状态"></el-input>
    </el-form-item>
    <el-form-item label="负责人" prop="chargepeople">
      <el-input v-model="dataForm.chargepeople" placeholder="负责人"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          fid: 0,
          pid: '',
          jname: '',
          city: '',
          jtype: '',
          jlevel: '',
          status: '',
          chargepeople: ''
        },
        dataRule: {
          pid: [
            { required: true, message: '包号不能为空', trigger: 'blur' }
          ],
          jname: [
            { required: true, message: '机构名称不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '省份城市不能为空', trigger: 'blur' }
          ],
          jtype: [
            { required: true, message: '机构类型不能为空', trigger: 'blur' }
          ],
          jlevel: [
            { required: true, message: '机构级别不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '分包状态不能为空', trigger: 'blur' }
          ],
          chargepeople: [
            { required: true, message: '负责人不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.fid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.fid) {
            this.$http({
              url: this.$http.adornUrl(`/com/fpackageinformation/info/${this.dataForm.fid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.pid = data.fpackageInformation.pid
                this.dataForm.jname = data.fpackageInformation.jname
                this.dataForm.city = data.fpackageInformation.city
                this.dataForm.jtype = data.fpackageInformation.jtype
                this.dataForm.jlevel = data.fpackageInformation.jlevel
                this.dataForm.status = data.fpackageInformation.status
                this.dataForm.chargepeople = data.fpackageInformation.chargepeople
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/com/fpackageinformation/${!this.dataForm.fid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'fid': this.dataForm.fid || undefined,
                'pid': this.dataForm.pid,
                'jname': this.dataForm.jname,
                'city': this.dataForm.city,
                'jtype': this.dataForm.jtype,
                'jlevel': this.dataForm.jlevel,
                'status': this.dataForm.status,
                'chargepeople': this.dataForm.chargepeople
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
